<?php

namespace App\Http\Controllers\Api;

use App\Models\Company;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Requests\Api\CompanyRequest;
use App\Transformers\CompanyTransformer;

class CompaniesController extends Controller
{
    public function search(Request $request, Company $company)
    {
        $per_page = $request->input('per_page', 20);
        $query = $company->query();

        if ($areaId = $request->area_id) {
            $query->where('area_id', $areaId);
        }

        if ($name = $request->name) {
            $query=$query->where('name', 'like', "%{$request->name}%");
        }

        if ($name = $request->cate_id) {
            $query=$query->whereHas('categories', function($q) use ($request)
                {
                    $q->where('id',  $request->cate_id);
                });
        }

        $query->where('delflag', false);

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $companies = $query->paginate($per_page);

        return $this->response->paginator($companies, new CompanyTransformer());
    }
    public function index(Request $request, Company $company)
    {
        $per_page = $request->input('per_page', 20);
        $query = $company->query();

        if ($areaId = $request->area_id) {
            $query->where('area_id', $areaId);
        }

        $query->where('delflag', false);

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $companies = $query->paginate($per_page);

        return $this->response->paginator($companies, new CompanyTransformer());
    }

    public function userIndex(Request $request)
    {
        $per_page = $request->input('per_page', 20);

        $companies=null;

        if ($orderBy = $request->orderBy) {
            $companies = $this->user->companies()->where('delflag', false)->ordered()
                    ->paginate($per_page);
        } else {
            $companies = $this->user->companies()->where('delflag', false)->recent()
                    ->paginate($per_page);
        }

        return $this->response->paginator($companies, new CompanyTransformer());
    }

    public function show(Company $company)
    {
        return $this->response->item($company, new CompanyTransformer());
    }

    public function store(CompanyRequest $request, Company $company)
    {
        $this->authorize('update', $company);
        $company->fill($request->all());

        $company->save();

        return $this->response->item($company, new CompanyTransformer())
            ->setStatusCode(201);
    }

    public function update(CompanyRequest $request, Company $company)
    {
        $this->authorize('update', $company);
        $company->update($request->all());

        return $this->response->item($company, new CompanyTransformer());
    }

    public function destroy(Company $company)
    {
        $this->authorize('destroy', $company);
        $company->delflag=true;
        $company->save();
        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'删除成功',
        ])->setStatusCode(200);
    }

    public function addAdmin(Request $request, Company $company)
    {
        $this->authorize('update', $company);
        $company->users()->attach($request->user_id);

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'添加商家管理员成功',
        ])->setStatusCode(200);
    }

    public function addCategory(Request $request, Company $company)
    {
        $this->authorize('update', $company);
        $company->categories()->sync($request->cate_ids);
        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'分类修改成功',
        ])->setStatusCode(200);
    }
}